package com.wll.exportfile.world.poitl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopColumnTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.wll.exportfile.world.entity.Goods;
import com.wll.exportfile.world.entity.Labor;
import com.wll.exportfile.world.entity.PaymentHackData;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class LoopColumnTableRenderPolicyTest {

    public static void main(String[] args) throws Exception {
        ClassLoader classloader =
                org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
        URL res = classloader.getResource(
                "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
        String path = res.getPath();
        System.out.println("Core POI came from " + path);

        String resource = "E://my_work//study//exportFile//src//main//resources//world//deepoove//render_loopcolumn.docx";
        PaymentHackData data = new PaymentHackData();

        data.setTotal("总共：7200");

        List<Goods> goods = new ArrayList<>();
        Goods good = new Goods();
        good.setCount(4);
        good.setName("墙纸");
        good.setDesc("书房卧室");
        good.setDiscount(1500);
        good.setPrice(400);
        good.setTax(new Random().nextInt(10) + 20);
        good.setTotalPrice(1600);
        good.setPicture(Pictures.ofLocal("E://my_work//study//exportFile//src//main//resources//world//deepoove//earth.png").size(24, 24).create());
//        good.setPicture(Pictures.ofUrl("http://deepoove.com/images/icecream.png").size(24, 24).create());
        goods.add(good);
        goods.add(good);
        goods.add(good);
        goods.add(good);
        data.setGoods(goods);

        List<Labor> labors = new ArrayList<>();
        Labor labor = new Labor();
        labor.setCategory("油漆工");
        labor.setPeople(2);
        labor.setPrice(400);
        labor.setTotalPrice(1600);
        labors.add(labor);
        labors.add(labor);
        labors.add(labor);
        labors.add(labor);
        data.setLabors(labors);

        // same line
        data.setGoods2(goods);
        data.setLabors2(labors);

        LoopColumnTableRenderPolicy hackLoopTableRenderPolicy = new LoopColumnTableRenderPolicy();
        RenderPolicy hackLoopSameLineTableRenderPolicy = new LoopColumnTableRenderPolicy(true);
        Configure config = Configure.builder()
                .bind("goods", hackLoopTableRenderPolicy)
                .bind("labors", hackLoopTableRenderPolicy)
                .bind("goods2", hackLoopSameLineTableRenderPolicy)
                .bind("labors2", hackLoopSameLineTableRenderPolicy)
                .build();
        XWPFTemplate template = XWPFTemplate.compile(resource, config).render(data);
        template.writeToFile("E:/temp/out_render_loopcolumn.docx");
    }
}
